package org.example.flea_market_mvc.service.shoppingcarts.impl;

import org.example.flea_market_mvc.entity.ShoppingCarts;
import org.example.flea_market_mvc.mapper.ShoppingCartsMapper;
import org.example.flea_market_mvc.param.ShoppingCartParam;
import org.example.flea_market_mvc.service.shoppingcarts.ShoppingCartsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 购物车服务实现类
 * 处理购物车业务逻辑，支持登录用户和游客购物车管理
 */
@Service
public class ShoppingCartsServiceImpl implements ShoppingCartsService {

    @Autowired
    private ShoppingCartsMapper shoppingCartsMapper;

    @Override
    public int insert(ShoppingCartParam shoppingCartParam) {
        ShoppingCarts cart = new ShoppingCarts();
        BeanUtils.copyProperties(shoppingCartParam, cart);
        return shoppingCartsMapper.insert(cart);
    }

    @Override
    public int deleteById(Integer id) {
        return shoppingCartsMapper.deleteById(id);
    }

    @Override
    public int update(ShoppingCartParam shoppingCartParam) {
        ShoppingCarts cart = new ShoppingCarts();
        BeanUtils.copyProperties(shoppingCartParam, cart);
        return shoppingCartsMapper.updateById(cart);
    }

    @Override
    public ShoppingCarts selectById(Integer id) {
        return shoppingCartsMapper.selectById(id);
    }

    @Override
    public List<ShoppingCarts> selectAll() {
        return shoppingCartsMapper.selectList(null);
    }

    @Override
    public List<ShoppingCarts> selectByUserId(Integer userId) {
        return shoppingCartsMapper.selectByUserId(userId);
    }

    @Override
    public ShoppingCarts selectByUserIdAndProductId(Integer userId, Integer productId) {
        return shoppingCartsMapper.selectByUserIdAndProductId(userId, productId);
    }

    @Override
    public List<ShoppingCarts> selectBySessionId(String sessionId) {
        return shoppingCartsMapper.selectBySessionId(sessionId);
    }

    @Override
    public int updateStatusBatch(List<Integer> cartIds, Integer status) {
        return shoppingCartsMapper.updateStatusBatch(cartIds, status);
    }

    @Override
    public int deleteByUserId(Integer userId) {
        return shoppingCartsMapper.deleteByUserId(userId);
    }

    @Override
    public int updateCartQuantity(Integer id, Integer data) {
        return shoppingCartsMapper.updateCartQuantity(id,data);
    }

}